﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Collections;
using Common;
using MainSystemForm.datamanage;
using MainSystemForm.handler;

namespace MainSystemForm
{
    public partial class DataManageForm : Form
    {
        SQLHelper helper = new SQLHelper();
        SystemConfig config = new SystemConfig();
        public DataManageForm()
        {
            InitializeComponent();
        }

        private void DataManageForm_Load(object sender, EventArgs e)
        {
            this.SetBounds((Screen.GetBounds(this).Width / 2) - (this.Width / 2), (Screen.GetBounds(this).Height / 2) - (this.Height / 2), this.Width, this.Height, BoundsSpecified.Location);
            tabControl.TabPages.Remove(BlackCarTab);
            tabControl.TabPages.Remove(PassRecordTab);
            tabControl.TabPages.Remove(IllegalRecordTab);
            tabControl.TabPages.Remove(PassRecordReportTab);
            tabControl.TabPages.Remove(UserManageTab);
        }

        private void ExitItem_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void CarInfoManageItem_Click(object sender, EventArgs e)
        {
            if (!tabControl.TabPages.Contains(CarManageTab))
            {
                tabControl.TabPages.Add(CarManageTab);
            }
            
            tabControl.TabPages.Remove(BlackCarTab);
            tabControl.TabPages.Remove(PassRecordTab);
            tabControl.TabPages.Remove(IllegalRecordTab);
            tabControl.TabPages.Remove(PassRecordReportTab);
            tabControl.TabPages.Remove(UserManageTab);
        }

        private void BlackCarManageItem_Click(object sender, EventArgs e)
        {
            if (!tabControl.TabPages.Contains(BlackCarTab))
            {
                tabControl.TabPages.Add(BlackCarTab);
            }
            tabControl.TabPages.Remove(CarManageTab);
            tabControl.TabPages.Remove(PassRecordTab);
            tabControl.TabPages.Remove(IllegalRecordTab);
            tabControl.TabPages.Remove(PassRecordReportTab);
            tabControl.TabPages.Remove(UserManageTab);
        }

        private void PassRecordListItem_Click(object sender, EventArgs e)
        {
            if (!tabControl.TabPages.Contains(PassRecordTab))
            {
                tabControl.TabPages.Add(PassRecordTab);
            }
            tabControl.TabPages.Remove(CarManageTab);
            tabControl.TabPages.Remove(BlackCarTab);
            tabControl.TabPages.Remove(IllegalRecordTab);
            tabControl.TabPages.Remove(PassRecordReportTab);
            tabControl.TabPages.Remove(UserManageTab);

            //PassRecordPassPlace.Text = config.PassPlace;
        }

        private void IllegalRecordListItem_Click(object sender, EventArgs e)
        {
            if (!tabControl.TabPages.Contains(IllegalRecordTab))
            {
                tabControl.TabPages.Add(IllegalRecordTab);
            }
            tabControl.TabPages.Remove(CarManageTab);
            tabControl.TabPages.Remove(BlackCarTab);
            tabControl.TabPages.Remove(PassRecordTab);
            tabControl.TabPages.Remove(PassRecordReportTab);
            tabControl.TabPages.Remove(UserManageTab);
        }

        private void PassRecordReportItem_Click(object sender, EventArgs e)
        {
            if (!tabControl.TabPages.Contains(PassRecordReportTab))
            {
                tabControl.TabPages.Add(PassRecordReportTab);
            }
            tabControl.TabPages.Remove(CarManageTab);
            tabControl.TabPages.Remove(BlackCarTab);
            tabControl.TabPages.Remove(PassRecordTab);
            tabControl.TabPages.Remove(IllegalRecordTab);
            tabControl.TabPages.Remove(UserManageTab);
        }

        private void UserInfoManageItem_Click(object sender, EventArgs e)
        {
            if (!tabControl.TabPages.Contains(UserManageTab))
            {
                tabControl.TabPages.Add(UserManageTab);
            }
            tabControl.TabPages.Remove(CarManageTab);
            tabControl.TabPages.Remove(BlackCarTab);
            tabControl.TabPages.Remove(PassRecordTab);
            tabControl.TabPages.Remove(IllegalRecordTab);
            tabControl.TabPages.Remove(PassRecordReportTab);

            
            string sql = "select * from users";
            DataTable dt = helper.Selectinfo(sql);
            UserInfoGrid.DataSource = dt;
            
        }

        private void VersionInfoItem_Click(object sender, EventArgs e)
        {
            Form form = new SystemVersionForm();
            form.Show();
        }

        private void UserSearchTextBox_TextChanged(object sender, EventArgs e)
        {

        }

        private void button2_Click(object sender, EventArgs e)
        {
            string sql = "";
            if (UserSearchTextBox.Text == "")
            {
                sql = "select * from users";
            }
            else
            {
                sql = string.Format("select * from users where FsLoginName like '%{0}%'",UserSearchTextBox.Text);
            }
            
            DataTable dt = helper.Selectinfo(sql);
            UserInfoGrid.DataSource = dt;
            if (dt.Rows.Count == 0)
            {
                MessageBox.Show("未检索到用户信息！");
            }
        }

        private void button3_Click(object sender, EventArgs e)
        {
            UserLoginName.Text = "";
            UserPassword.Text = "";
        }

        private void button4_Click(object sender, EventArgs e)
        {
            DateTime startTime = new DateTime(PassRecordInDate.Value.Year, PassRecordInDate.Value.Month, PassRecordInDate.Value.Day, 0, 0, 0);
            DateTime endTime = new DateTime(PassRecordOutDate.Value.Year, PassRecordOutDate.Value.Month, PassRecordOutDate.Value.Day, 23,59,59);

            string sql = string.Format("select FsPlate, case FsPassType when 'in' then '驶入' when 'out' then '驶出' end FsPassType, FsPassPlace, FdPassDateTime, FsPhotoName from passrecords where FsPassPlace = '{0}' and FdPassDateTime between '{1}' and '{2}' ", PassRecordPassPlace.Text, startTime, endTime);
            if (PassRecordPlate.Text != "")
            {
                sql += " and FsPlate like '%" + PassRecordPlate.Text + "%'";
            }
            if(PassRecordCarType.Text == "内部车辆")
            {
                sql += " and FsPlate in (select FsPlate from cars)"; 
            }
            else if (PassRecordCarType.Text == "外部车辆")
            {
                sql += " and FsPlate not in (select FsPlate from cars)";
            }

            if (PassRecordPassType.Text == "驶入")
            {
                sql += " and FsPassType = 'in'";
            }
            else if (PassRecordPassType.Text == "驶出")
            {
                sql += " and FsPassType = 'out'";
            }
            DataTable dt = helper.Selectinfo(sql);
            PassRecordGridView.DataSource = dt;
            if (dt.Rows.Count == 0)
            {
                MessageBox.Show("未检索到车辆进出记录信息！");
            }
        }
    }
}